﻿using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Triples {

    public class AppSettings {
        // Our settings
        IsolatedStorageSettings settings;

        // The key names of our settings
        const string AwsAccessKeySettingKeyName = "AwsAccessKey";
        const string AwsSecretKeySettingKeyName = "AwsSecretKey";
        const string AwsRegionSettingKeyName = "AwsRegion";

        // The default value of our settings
        const string AwsAccessKeySettingDefault = "AKIAJVVIHNWT4CFV4GMQ";
        const string AwsSecretKeySettingDefault = "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";
        const string AwsRegionSettingDefault = "us-west-2";

        /// <summary>
        /// Constructor that gets the application settings.
        /// </summary>
        public AppSettings() {
            // Get the settings for this application.
            settings = IsolatedStorageSettings.ApplicationSettings;
        }

        /// <summary>
        /// Update a setting value for our application. If the setting does not
        /// exist, then add the setting.
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool AddOrUpdateValue(string Key, Object value) {
            bool valueChanged = false;

            // If the key exists
            if (settings.Contains(Key)) {
                // If the value has changed
                if (settings[Key] != value) {
                    // Store the new value
                    settings[Key] = value;
                    valueChanged = true;
                }
            }
            // Otherwise create the key.
            else {
                settings.Add(Key, value);
                valueChanged = true;
            }
            return valueChanged;
        }

        /// <summary>
        /// Get the current value of the setting, or if it is not found, set the 
        /// setting to the default setting.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="Key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public T GetValueOrDefault<T>(string Key, T defaultValue) {
            T value;

            // If the key exists, retrieve the value.
            if (settings.Contains(Key)) {
                value = (T)settings[Key];
            }
            // Otherwise, use the default value.
            else {
                value = defaultValue;
            }
            return value;
        }

        /// <summary>
        /// Save the settings.
        /// </summary>
        public void Save() {
            settings.Save();
        }


        public string AwsAccessKey {
            get { return GetValueOrDefault<string>(AwsAccessKeySettingKeyName, AwsAccessKeySettingDefault); }
            set { if (AddOrUpdateValue(AwsAccessKeySettingKeyName, value)) Save(); }
        }


        public string AwsSecretKey {
            get { return GetValueOrDefault<string>(AwsSecretKeySettingKeyName, AwsSecretKeySettingDefault); }
            set { if (AddOrUpdateValue(AwsSecretKeySettingKeyName, value)) Save(); }
        }


        public string AwsRegion {
            get { return GetValueOrDefault<string>(AwsRegionSettingKeyName, AwsRegionSettingDefault); }
            set { if (AddOrUpdateValue(AwsRegionSettingKeyName, value)) Save(); }
        }
    }
}
